package column

import (
	"fmt"
	"game/game-db/orm/metag"
	"reflect"
	"strings"
	"unsafe"
)

type Int32ColumnMeta struct {
	ColumnMeta
}

func (t *Int32ColumnMeta) FillValue(objField reflect.Value, fieldValue reflect.Value) {
	if objField.CanSet() {
		objField.SetInt(fieldValue.Interface().(int64))
	} else {
		objField = reflect.NewAt(objField.Type(), unsafe.Pointer(objField.UnsafeAddr())).Elem()
		objField.SetInt(fieldValue.Interface().(int64))
	}
}
func (t *Int32ColumnMeta) TakeValue(objField reflect.Value) interface{} {
	if objField.CanInterface() {
		return objField.Interface()
	} else {
		objField = reflect.NewAt(objField.Type(), unsafe.Pointer(objField.UnsafeAddr())).Elem()
		return objField.Interface()
	}
}

func (t *Int32ColumnMeta) DbColumnType() string {
	return "int"
}

func (t *Int32ColumnMeta) DefaultValue() string {
	if t.defaultValue == "" {
		return "DEFAULT 0"
	} else {
		return fmt.Sprintf("DEFAULT %s", t.defaultValue)
	}
}

func (t *Int32ColumnMeta) IsChange(dbColumnType string, dbColumnExtra string) bool {
	return !strings.HasPrefix(dbColumnType, "int")
}

func NewInt32Field(structField reflect.StructField, columnMetag metag.ColumnMetag) IColumnMeta {
	var meta Int32ColumnMeta
	meta.fillBaseValue(structField, columnMetag)
	return &meta
}
